package org.testis.utils.middleware;

import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_test_log_header;
import org.testis.orm.classes.T_test_log_headerCriteria;

/**
 * 
 * @author nurhat
 */
public class LogHeader extends DbConnectionSupplier {

    public synchronized void commit() {
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		t.commit();
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
    }

    public synchronized void addLog(int designTreeId, int designPartType,
	    int result, int parentId, String name, String dateExecuted,
	    String guid) {
	try {

	    T_test_log_header logHeader = new T_test_log_header();
	    logHeader.setDesignTreeId(designTreeId);
	    logHeader.setDesignPartType(designPartType);
	    logHeader.setResult(result);
	    logHeader.setParentId(parentId);
	    logHeader.setName(name);
	    logHeader.setDateExecuted(dateExecuted);
	    logHeader.setGuid(guid);
	    logHeader.setExecutionGuid("DOLDUR!");
	    logHeader.save();
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
    }

    public synchronized void addLog(int designTreeId, int designPartType,
	    int result, int parentId, String name, String dateExecuted,
	    String guid, String executionGuid) {
	try {
	    T_test_log_headerCriteria headerCriteria = new T_test_log_headerCriteria();
	    headerCriteria.executionGuid.eq(executionGuid);
	    headerCriteria.designTreeId.eq(designTreeId);
	    headerCriteria.designPartType.eq(designPartType);
	    headerCriteria.guid.eq(guid);
	    T_test_log_header logHeader = headerCriteria
		    .uniqueT_test_log_header();
	    if (logHeader == null) {
		logHeader = new T_test_log_header();
	    }

	    logHeader.setDesignTreeId(designTreeId);
	    logHeader.setDesignPartType(designPartType);
	    logHeader.setResult(result);
	    logHeader.setParentId(parentId);
	    logHeader.setName(name);
	    logHeader.setDateExecuted(dateExecuted);
	    logHeader.setGuid(guid);
	    logHeader.setExecutionGuid(executionGuid);
	    logHeader.save();
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
    }

    public synchronized void addLog(int designTreeId, int designPartType,
	    int result, String parentGuid, String name, String dateExecuted,
	    String guid, String executionGuid) {
	try {
	    T_test_log_headerCriteria headerCriteria = new T_test_log_headerCriteria();
	    headerCriteria.executionGuid.eq(executionGuid);
	    headerCriteria.designTreeId.eq(designTreeId);
	    headerCriteria.designPartType.eq(designPartType);
	    headerCriteria.guid.eq(guid);
	    T_test_log_header logHeader = headerCriteria
		    .uniqueT_test_log_header();
	    if (logHeader == null) {
		logHeader = new T_test_log_header();
	    }

	    logHeader.setDesignTreeId(designTreeId);
	    logHeader.setDesignPartType(designPartType);
	    logHeader.setResult(result);
	    logHeader.setParentGuid(parentGuid);
	    logHeader.setName(name);
	    logHeader.setDateExecuted(dateExecuted);
	    logHeader.setGuid(guid);
	    logHeader.setExecutionGuid(executionGuid);
	    logHeader.save();
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
    }
}
